const api="http://47.106.183.53:9090";

const request= (url, method, data,token) =>{
  let _url=api+url;
  data = data || {};
  token=token||"";
  return new Promise((resolve, reject) => {
    wx.request({
      url: _url,
      method: method,
      data: data,
      header:{
        token:token,
        'content-type': 'application/x-www-form-urlencoded'
      },
      success(request) {
        console.log("请求完成的结果:")
        console.log(request)
        if (request.code == 200) {
          resolve(request.data);
        } else if (request.code == 204) {
          resolve(request)
        } else {
          resolve(request)
        }
      },
      fail(error) {
        console.log("发生错误:")
        console.log(error)
        reject(error)
        wx.showToast({
          title: '网络有点不给力哦',
          icon:'none'
        })
      },
      complete(e) {
        // 加载完成
      }
    })
  })
}

module.exports ={
  //注册
  miniRegister(data){
    return request('/wx/register',"post",data,"")
  },
  //登录
  miniLogin(data){
    return request('/wx/login?code='+data.code,'get',"","")
  },
  //获取二维码
  qrcode(token){
    return request('/wx/user/qrcode','get',"",token)
  },
  //更新个人信息
  user(data,token){
    return request('/wx/user','post',data,token)
  },
  //获取个人信息
  getUser(token){
    return request('/wx/user','get',"",token)
  },
  //扫码后记录用户信息
  healthRecord(data,token){
    return request('/wx/health_record','post',data,token)
  },
  //获取扫码记录
  healthRecords(data,token){
    return request('/wx/health_record','get',data,token)
  },
  //添加审核记录
  examine(data,token){
    return request('/wx/examine','post',data,token);
  },
  //获取审核记录
  getExamine(data,token){
    return request('/wx/examine/'+data,'get','',token);
  }
}